﻿using System;
using System.Runtime.InteropServices;

namespace PokeTD
{
    public class CursorClip
    {
        #region DllImport
        public struct RECT
        {
            public int x;
            public int y;
            public int w;
            public int h;
        };

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetClipCursor")]
        static extern bool GetClipCursor(out RECT window_rect);
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowRect")]
        static extern bool GetWindowRect(IntPtr hwnd, out RECT window_rect);
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "ClipCursor")]
        static extern bool ClipCursor(out RECT window_rect);
        #endregion

        static RECT OldClipRect;

        public static void ClipCursor(IntPtr hwnd)
        {
            RECT window_rect;
            GetWindowRect(hwnd, out window_rect);
            ClipCursor(window_rect);
        }
        public static void ClipCursor(int x, int y, int width, int height)
        {
            RECT r;
            r.x = x;
            r.y = y;
            r.w = width;
            r.h = height;
            ClipCursor(r);
        }
        public static void ClipCursor(RECT window_rect)
        {
            GetClipCursor(out OldClipRect);
            ClipCursor(out window_rect);
        }

        public static void UnclipCursor()
        {
            ClipCursor(out OldClipRect);
        }
    }
}
